﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Collections;

namespace DAO
{
    public class KhachHangDAO
    {
        /// <summary>
        /// Lay tat ca thong tin cua khach hang theo ma khach hang
        /// </summary>
        /// <param name="makh">Ma khach hang</param>
        /// <returns>null - neu khong co khach hang do</returns>
        public KhachHangDTO layThongTinKhachHangTheoMaKH(string makh)
        {
            List<String> paraArr = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraArr.Add("@makh");
            valueArr.Add(makh);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_KhachHang_layThongTinKhachHangTheoMaKH", paraArr, valueArr);
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    KhachHangDTO kh = new KhachHangDTO();
                    kh.MaKhachHang = dt.Rows[0]["MaKhachHang"].ToString();
                    kh.MaTaiKhoan = dt.Rows[0]["MaTaiKhoan"].ToString();
                    kh.HoTen = dt.Rows[0]["HoTen"].ToString();
                    kh.NgayDangKy = Convert.ToDateTime(dt.Rows[0]["NgayDangKy"].ToString());
                    kh.SDT = dt.Rows[0]["SDT"].ToString();
                    kh.LoaiThe = dt.Rows[0]["LoaiThe"].ToString();
                    kh.MaSoThe = dt.Rows[0]["MaSoThe"].ToString();
                    kh.CMND = dt.Rows[0]["CMND"].ToString();
                    kh.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                    kh.Email = dt.Rows[0]["Email"].ToString();
                    return kh;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            
        }

        /// <summary>
        /// Lay tat ca thong tin cua khach hang theo ma tai khoan
        /// </summary>
        /// <param name="matk">Ma tai khoan</param>
        /// <returns>null - neu khong co khach hang do</returns>
        public KhachHangDTO layThongTinKhachHangTheoMaTK(string matk)
        {
            List<String> paraArr = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraArr.Add("@matk");
            valueArr.Add(matk);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_KhachHang_layThongTinKhachHangTheoMaTK", paraArr, valueArr);
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    KhachHangDTO kh = new KhachHangDTO();
                    kh.MaKhachHang = dt.Rows[0]["MaKhachHang"].ToString();
                    kh.MaTaiKhoan = dt.Rows[0]["MaTaiKhoan"].ToString();
                    kh.HoTen = dt.Rows[0]["HoTen"].ToString();
                    kh.NgayDangKy = Convert.ToDateTime(dt.Rows[0]["NgayDangKy"].ToString());
                    kh.SDT = dt.Rows[0]["SDT"].ToString();
                    kh.LoaiThe = dt.Rows[0]["LoaiThe"].ToString();
                    kh.MaSoThe = dt.Rows[0]["MaSoThe"].ToString();
                    kh.CMND = dt.Rows[0]["CMND"].ToString();
                    kh.DiaChi = dt.Rows[0]["DiaChi"].ToString();
                    kh.Email = dt.Rows[0]["Email"].ToString();
                    return kh;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Them khach hang moi khi da co ma tai khoan
        /// </summary>
        /// <param name="kh">Khach hang</param>
        /// <returns>ma khach hang - null neu khong them thanh cong</returns>
        /// <proc>sp_KhachHang_themKhachHang</proc>
        public string themKhachHang(KhachHangDTO kh)
        {
            List<string> paraLst = new List<string>();
            ArrayList valueArr = new ArrayList();
            paraLst.Add("@matk");
            paraLst.Add("@hoten");
            paraLst.Add("@cmnd");
            paraLst.Add("@diachi");
            paraLst.Add("@email");
            paraLst.Add("@sdt");
            paraLst.Add("@msthe");
            paraLst.Add("@loaithe");
            valueArr.Add(kh.MaTaiKhoan);
            valueArr.Add(kh.HoTen);
            valueArr.Add(kh.CMND);
            valueArr.Add(kh.DiaChi);
            valueArr.Add(kh.Email);
            valueArr.Add(kh.SDT);
            valueArr.Add(kh.MaSoThe);
            valueArr.Add(kh.LoaiThe);
            try
            {
                DataTable dt = DataProvider.ExecuteStoreProc("sp_KhachHang_themKhachHang", paraLst, valueArr);
                if (dt.Rows.Count == 0)
                    return null;
                return dt.Rows[0]["MaKhachHang"].ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
